package service;

import dao.*;
import model.Game;
import model.Level;
import model.Player;
import model.Rating;

import java.util.Iterator;
import java.util.List;

/**
 * Created by VLAD on 07.07.2014.
 */
public class PlayerService
{
    IPlayerDao playerDao = new PlayerDao();
    ILevelDao levelDao = new LevelDao();
    IRatingDao ratingDao = new RatingDao();


    public Player savePlayer(String nick){
        Rating rating = new Rating();
        Level level = levelDao.getLevelByID(1);
        Player player = new Player(nick, rating, level);
        ratingDao.saveRatio(rating);
        playerDao.savePlayer(player);
        return player;
    }

    public void addLevel(long points){
        Level level = new Level(points);
        levelDao.saveLevel(level);
    }

    public void showAllPlayersInfo()
    {
        List players = playerDao.getAllPlayers();
        Iterator iter = players.iterator();
        while (iter.hasNext())
        {
            System.out.println(iter.next());
        }
    }

    public void updatePlayer(Player player)
    {
        Level level = levelDao.getLevelByPoints(player.getCurrentPoints());
        player.setLevel(level);
        Rating rating = player.getRating();
        if(rating.getPlayed() != 0)
        {
            rating.setRating(rating.getWon()*100/rating.getPlayed());
        }

        playerDao.updatePlayer(player);
    }

    public Player getPlayerByID(long id) {
        return playerDao.getPlayerByID(id);
    }

    public List getAllByRatingOrder(int listCapacity)
    {
        return playerDao.getAllByRatingOrder(listCapacity);
    }

    public long getPlayerPositionInRating(Player player)
    {
        return playerDao.getPlayerPositionInRating(player);
    }

    public void addGameToPlayer(Player player, Game game)
    {
        player.setGame(game);
        playerDao.updatePlayer(player);

    }

    public void createLevel(long points)
    {
        levelDao.saveLevel(new Level(points));
    }




}
